package activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;

import mytool.Distance;
import mytool.GaussXYDeal;
import mytool.Gps2BaiDu;
import mytool.HttpSend;
import mytool.Itude;
import mytool.JsonParser;
import mytool.PrintLog;
import mytool.ToastShow;
import mytool.jsonpackage.RootCode;
import tool.ducong.com.mygpsdemo.MyAPP;
import tool.ducong.com.mygpsdemo.R;

/**
 * Created by zerdoor_pc on 2016/3/3.
 */
public class MobileActivity extends Activity {
    private TextView mobile_distance,mobile_longitude, mobile_latitude, mobile_differential_x, mobile_differential_y, mobile_revised_longitude, mobile_revised_latitude, mobile_flag;
    private Button open_map,add_point;
    private Boolean Whethercanjump = false;
    public static double[] mitude;
    private DecimalFormat df;
    private Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    inIt();
                    break;
                case 2:
                    PrintLog.i("HTTPURL:3==" + msg.obj);
                    if (JsonParser.CheckCode((String) msg.obj)) {
                        RootCode root = JsonParser.getAppli((String) msg.obj);
                        if (!root.msg.equalsIgnoreCase("success")) {
                            mobile_flag.append("\n获取基站差分值失败！");
                            Whethercanjump = false;
                            return;
                        }
                        if (root.data != null) {
                            //差分后经纬度
                            mobile_flag.append("\n获取基站差分值成功！");
                            mobile_flag.append("\n正在计算差分经纬...");

                            Itude itude = GaussXYDeal.GaussToBLToGauss(MyAPP.itude.getLongitude(), MyAPP.itude.getLatitude());

                            double mlatitude = itude.getLatitude() - root.data.carin_x;
                            double mlongitude = itude.getLongitude() - root.data.carin_y;
                            mitude=null;
                            mitude = GaussXYDeal.GaussToBL(mlatitude, mlongitude);
                            ;
//                            mobile_revised_longitude.append(df.format(mitude[0]) + "");
//                            mobile_revised_latitude.append(df.format(mitude[1]) + "");
                            mobile_revised_longitude.setText(R.string.longitude);
                            mobile_revised_latitude.setText(R.string.latitude);
                            mobile_revised_longitude.append(mitude[0] + "\n      度分秒：" + Gps2BaiDu.ConvertDigitalToDegrees(mitude[0]));
                            mobile_revised_latitude.append(mitude[1] + "\n       度分秒：" + Gps2BaiDu.ConvertDigitalToDegrees(mitude[1]));

                            mobile_differential_x.setText(R.string.x);
                            mobile_differential_y.setText(R.string.y);
                            mobile_differential_x.append(root.data.carin_x + "");
                            mobile_differential_y.append(root.data.carin_y + "");
                            mobile_distance.setText(R.string.distance);
                            mobile_distance.append(Distance.getDisance(MyAPP.itude.getLatitude(), MyAPP.itude.getLongitude(), mitude[1], mitude[0]) + "");
                            mobile_flag.append("\n差分经纬度设置成功！\n 现在可以点击下方的按钮打开小地图查看位置信息!");
                            Whethercanjump = true;

                        } else {
                            mobile_flag.append("\n获取基站差分值失败！");
                            Whethercanjump = false;
                        }

                    }
                    /*if (MyAPP.differentialitude!=null){
                        //差分后经纬度
                        Itude itude= GaussXYDeal.GaussToBLToGauss(MyAPP.itude.getLongitude(), MyAPP.itude.getLatitude());
                        double mlatitude= itude.getLatitude()-MyAPP.differentialitude.getLatitude();
                        double mlongitude= itude.getLongitude()-MyAPP.differentialitude.getLongitude();
                        double[] mitude= GaussXYDeal.GaussToBL(mlatitude,mlongitude);
                        mobile_revised_longitude.append(mitude[0]+"");
                        mobile_revised_latitude.append(mitude[1]+"");
                    }else{
                        Toast.makeText(MobileActivity.this,"获取差分值失败！",Toast.LENGTH_SHORT).show();
                    }
                    if (MyAPP.differentialitude!=null){
                        mobile_differential_x.setText(R.string.x);
                        mobile_differential_y.setText(R.string.y);
                        mobile_differential_x.append(MyAPP.differentialitude.getLatitude() + "");
                        mobile_differential_y.append(MyAPP.differentialitude.getLongitude() + "");
                    }*/
                    break;
                default:
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mobileactivity_item);
     /*   double lat=Gps2BaiDu.ConvertDegreesToDigital("30°34′13.91684″");
        double lon=Gps2BaiDu.ConvertDegreesToDigital("104°03′31.00057″");
//        double lat= 30.570495625;
//        double lon= 104.05860551388889;
        Itude itude2 = GaussXYDeal.GaussToBLToGauss(lon, lat);
        double[] itude3= GaussXYDeal.GaussToBL(itude2.getLatitude(),itude2.getLongitude());
//        Itude itude3 = GaussXYDeal.GaussToBLToGauss1(lon ,lat);
//        Itude itude4 = GaussXYDeal.GaussToBLToGauss2(lon,lat);
        PrintLog.i("坐标HTTPURL:原始坐标==" + lat + "\n" + lon);
        PrintLog.i("坐标HTTPURL:高斯坐标==" + itude2.getLatitude() + "\n" + itude2.getLongitude());
        PrintLog.i("坐标HTTPURL:反算坐标==" + itude3[1] + "\n" + itude3[0]);
//        PrintLog.i("坐标HTTPURL:高斯坐标2==" + itude4.getLatitude() + "\n" + itude4.getLongitude());*/
        getAllPoint();
        findView();
        inIt();

    }

    private void getAllPoint(){
        MyAPP.list_point=new ArrayList<>();
        MyAPP.list_point=MyAPP.LoadFileData2(MyAPP.LIST_POINT);
    }
    private void findView() {
        //实时
        mobile_longitude = (TextView) findViewById(R.id.mobile_longitude);
        mobile_latitude = (TextView) findViewById(R.id.mobile_latitude);
        //差分
        mobile_differential_x = (TextView) findViewById(R.id.mobile_differential_x);
        mobile_differential_y = (TextView) findViewById(R.id.mobile_differential_y);

        //修正
        mobile_revised_longitude = (TextView) findViewById(R.id.mobile_revised_longitude);
        mobile_revised_latitude = (TextView) findViewById(R.id.mobile_revised_latitude);
        mobile_distance = (TextView) findViewById(R.id.mobile_distance);
        mobile_flag = (TextView) findViewById(R.id.mobile_flag);
        open_map = (Button) findViewById(R.id.open_map);
        add_point = (Button) findViewById(R.id.add_point);
        open_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrintLog.i("HTTPURL" + MyAPP.LIST_POINT + ":" + MyAPP.list_point);
                if (Whethercanjump) {
                    Intent i = new Intent(MobileActivity.this, LayersDemo.class);
                    Bundle bun = new Bundle();
                    bun.putStringArray("original", new String[]{MyAPP.itude.getLongitude() + "", MyAPP.itude.getLatitude() + ""});
                    bun.putStringArray("now", new String[]{mitude[0] + "", mitude[1] + ""});
                    i.putExtras(bun);
                    startActivity(i);
                }else{
                    ToastShow.showShort(MobileActivity.this,"数据异常，无法开启地图！");
                }
            }
        });
        add_point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PrintLog.i("HTTPURL" + MyAPP.LIST_POINT + ":" + MyAPP.list_point);
                if (mitude==null)return;
                Itude itude=new Itude();
                itude.setLatitude(mitude[1]);
                itude.setLongitude(mitude[0]);
                if (MyAPP.list_point!=null&&MyAPP.Data.getMoreDATA(MyAPP.list_point,itude)) {
                    MyAPP.list_point.add(itude);
                    MyAPP.SaveFileData(MyAPP.LIST_POINT, MyAPP.list_point);//存储数据
                    PrintLog.i("HTTPURL" + MyAPP.LIST_POINT + ":" + MyAPP.list_point);
                }

            }
        });
        MyAPP.MobileActivityhandler = hander;
        //DecimalFormat限制小数点后面的位数
        df = new DecimalFormat("0.000000000000");
//        java.util.UUID

    }

    private void inIt() {

        mobile_flag.setText("正在初始化！");
        if (MyAPP.itude != null) {
            String itudegetLongitude=MyAPP.itude.getLongitude() + "";
            String itudegetLatitude=MyAPP.itude.getLatitude() + "";

            mobile_flag.append("\n正在设置实时经纬度。");
            mobile_longitude.setText(R.string.longitude);
            mobile_latitude.setText(R.string.latitude);
            mobile_revised_longitude.setText(R.string.longitude);
            mobile_revised_latitude.setText(R.string.latitude);
            mobile_longitude.append(itudegetLongitude);
            mobile_latitude.append(itudegetLatitude);
            mobile_flag.append("\n实时经纬度设置成功！");
            mobile_flag.append("\n正在获取基站差分值" + " \n原始基站距离：" +
                    " " + Distance.getDisance(Gps2BaiDu.ConvertDegreesToDigital("30°34′13.91684″"), Gps2BaiDu.ConvertDegreesToDigital("104°03′31.00057″"), 30.570495625, 104.05860551388889));//"30:34:13.91684N"+"104:03:31.00057E"
//                            104.05860551388889
//                            30.570495625);

            HttpSend.GetDifferenceData(hander, 2, itudegetLongitude, itudegetLatitude);

        } else {
            ToastShow.showShort(this, "获取经纬度失败！");
            mobile_flag.append("\n获取经纬度失败！");
            mobile_flag.append("\n初始化失败！");
        }

    }
}
